#include "mm.h"
#include "memberinfopage.h"
#include "addmemberpage.h"
#include "consumptiondetailpage.h"
#include "memberrechargepage.h"
#include "../database/database.h"
#include "../appinitializer.h"
#include <QDebug>
#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QtWidgets>  // 兜底引用Qt widgets组件（避免个别类未声明）

mm::mm(QWidget *parent) : QWidget(parent), m_db(nullptr), m_memberInfoPage(nullptr), m_addMemberPage(nullptr), m_consumptionDetailPage(nullptr), m_memberRechargePage(nullptr)
{
    // 设置窗口标题
    this->setWindowTitle("会员管理");

    // 设置窗口图标（可选）
    // this->setWindowIcon(QIcon(":/icons/member.png"));

    // 设置窗口大小策略
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    // 初始化数据库连接
    m_db = AppInitializer::database();

    // 初始化界面
    initUI();

    // 设置主界面最小尺寸，避免窗口过小导致组件错乱
    this->setMinimumSize(1000, 700);
}

mm::~mm()
{
    // Qt对象树机制：父对象销毁时自动删除子对象，无需手动delete指针
}

void mm::initUI()
{
    // 1. 主布局（垂直布局，占满整个窗口）
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setContentsMargins(0, 0, 0, 0);  // 消除默认边距（避免窗口边缘留白）
    mainLayout->setSpacing(0);                   // 消除布局内组件间距

    // 2. 创建顶部菜单并添加到主布局
    createTopMenu();
    if (m_topMenu) {  // 容错判断：若菜单创建成功才添加
        mainLayout->addWidget(m_topMenu);
    }

    // 3. 创建堆叠窗口（用于切换11个功能页面）
    m_stackedWidget = new QStackedWidget(this);
    if (m_stackedWidget) {
        // 伸缩因子设为1：让堆叠窗口占据主布局剩余所有空间（顶部菜单固定高度）
        mainLayout->addWidget(m_stackedWidget, 1);

        // -------------------------- 页面1：会员信息 --------------------------
        m_memberInfoPage = new MemberInfoPage(m_db, this);

        // -------------------------- 页面2：增加会员 --------------------------
        m_addMemberPage = new AddMemberPage(m_db, this);

        // -------------------------- 页面3：积分历史 --------------------------
        QWidget *page3 = new QWidget();
        page3->setStyleSheet("background-color: #CCCCFF;");  // 浅蓝色背景
        QLabel *label3 = new QLabel("积分历史页面", page3);
        label3->setAlignment(Qt::AlignCenter);
        label3->setStyleSheet("font-size: 16px; font-weight: bold;");
        QVBoxLayout *layout3 = new QVBoxLayout(page3);
        layout3->addWidget(label3);

        // -------------------------- 页面4：消费明细 --------------------------
        m_consumptionDetailPage = new ConsumptionDetailPage(this);

        // -------------------------- 页面5：收支明细 --------------------------
        QWidget *page5 = new QWidget();
        page5->setStyleSheet("background-color: #FFCCFF;");  // 浅粉色背景
        QLabel *label5 = new QLabel("收支明细页面", page5);
        label5->setAlignment(Qt::AlignCenter);
        label5->setStyleSheet("font-size: 16px; font-weight: bold;");
        QVBoxLayout *layout5 = new QVBoxLayout(page5);
        layout5->addWidget(label5);

        // -------------------------- 页面6：会员充值 --------------------------
        m_memberRechargePage = new MemberRechargePage(m_db, this);

        // -------------------------- 页面7：充值统计 --------------------------
        QWidget *page7 = new QWidget();
        page7->setStyleSheet("background-color: #FFD700;");  // 金色背景
        QLabel *label7 = new QLabel("充值统计页面", page7);
        label7->setAlignment(Qt::AlignCenter);
        label7->setStyleSheet("font-size: 16px; font-weight: bold;");
        QVBoxLayout *layout7 = new QVBoxLayout(page7);
        layout7->addWidget(label7);

        // -------------------------- 页面8：会员配置 --------------------------
        QWidget *page8 = new QWidget();
        page8->setStyleSheet("background-color: #DDA0DD;");  // 浅紫色背景
        QLabel *label8 = new QLabel("会员配置页面", page8);
        label8->setAlignment(Qt::AlignCenter);
        label8->setStyleSheet("font-size: 16px; font-weight: bold;");
        QVBoxLayout *layout8 = new QVBoxLayout(page8);
        layout8->addWidget(label8);

        // -------------------------- 页面9：商品积分设置 --------------------------
        QWidget *page9 = new QWidget();
        page9->setStyleSheet("background-color: #87CEFA;");  // 天蓝色背景
        QLabel *label9 = new QLabel("商品积分设置页面", page9);
        label9->setAlignment(Qt::AlignCenter);
        label9->setStyleSheet("font-size: 16px; font-weight: bold;");
        QVBoxLayout *layout9 = new QVBoxLayout(page9);
        layout9->addWidget(label9);

        // -------------------------- 页面10：折扣设置 --------------------------
        QWidget *page10 = new QWidget();
        page10->setStyleSheet("background-color: #98FB98;");  // 淡绿色背景
        QLabel *label10 = new QLabel("折扣设置页面", page10);
        label10->setAlignment(Qt::AlignCenter);
        label10->setStyleSheet("font-size: 16px; font-weight: bold;");
        QVBoxLayout *layout10 = new QVBoxLayout(page10);
        layout10->addWidget(label10);

        // -------------------------- 页面11：生日提醒 --------------------------
        QWidget *page11 = new QWidget();
        page11->setStyleSheet("background-color: #FFA07A;");  // 浅橙色背景
        QLabel *label11 = new QLabel("生日提醒页面", page11);
        label11->setAlignment(Qt::AlignCenter);
        label11->setStyleSheet("font-size: 16px; font-weight: bold;");
        QVBoxLayout *layout11 = new QVBoxLayout(page11);
        layout11->addWidget(label11);

        // 将所有页面添加到堆叠窗口（索引0~10，与按钮点击逻辑对应）
        m_stackedWidget->addWidget(m_memberInfoPage);  // 0: 会员信息
        m_stackedWidget->addWidget(m_addMemberPage);  // 1: 增加会员
        m_stackedWidget->addWidget(page3);  // 2: 积分历史
        m_stackedWidget->addWidget(m_consumptionDetailPage);  // 3: 消费明细
        m_stackedWidget->addWidget(page5);  // 4: 收支明细
        m_stackedWidget->addWidget(m_memberRechargePage);  // 5: 会员充值
        m_stackedWidget->addWidget(page7);  // 6: 充值统计
        m_stackedWidget->addWidget(page8);  // 7: 会员配置
        m_stackedWidget->addWidget(page9);  // 8: 商品积分设置
        m_stackedWidget->addWidget(page10); // 9: 折扣设置
        m_stackedWidget->addWidget(page11); // 10: 生日提醒

        // 默认显示第一个页面（会员信息）
        m_stackedWidget->setCurrentIndex(0);
    }
}

void mm::createTopMenu()
{
    // 1. 顶部菜单容器初始化（固定高度110px）
    m_topMenu = new QWidget(this);
    if (!m_topMenu) return;  // 容错：若容器创建失败，直接退出函数

    m_topMenu->setFixedHeight(110);
    m_topMenu->setStyleSheet("background-color: #E6F3FF; border-bottom: 1px solid #CCCCCC;");

    // 2. 顶部菜单主布局（水平布局，包含5个功能区）
    QHBoxLayout *mainMenuLayout = new QHBoxLayout(m_topMenu);
    mainMenuLayout->setContentsMargins(10, 5, 10, 5);  // 内边距：上下5px，左右10px
    mainMenuLayout->setSpacing(15);                   // 功能区间距15px

    // ====================== 功能区1：基本信息 ======================
    QWidget *area1 = new QWidget(m_topMenu);
    area1->setFixedHeight(100);
    area1->setStyleSheet("background-color: #FFFFFF; border: 1px solid #CCCCCC; border-radius: 4px;");
    QVBoxLayout *layout1 = new QVBoxLayout(area1);
    layout1->setContentsMargins(5, 5, 5, 5);
    layout1->setSpacing(5);

    // 功能区1按钮布局（水平排列2个按钮）
    QHBoxLayout *btnLayout1 = new QHBoxLayout();
    btnLayout1->setContentsMargins(0, 0, 0, 0);
    btnLayout1->setSpacing(10);  // 按钮间距10px

    m_memberInfoBtn = new QPushButton("会员信息", area1);
    m_memberInfoBtn->setFixedSize(60, 60);  // 固定按钮大小
    m_addMemberBtn = new QPushButton("增加会员", area1);
    m_addMemberBtn->setFixedSize(60, 60);

    btnLayout1->addWidget(m_memberInfoBtn);
    btnLayout1->addWidget(m_addMemberBtn);

    // 功能区标题（黄色背景，加粗文字）
    QLabel *label1 = new QLabel("基本信息", area1);
    label1->setFixedHeight(20);
    label1->setAlignment(Qt::AlignCenter);
    label1->setStyleSheet("background-color: #FFD700; color: #333333; font-weight: bold; border-radius: 2px;");

    layout1->addLayout(btnLayout1);
    layout1->addWidget(label1);
    mainMenuLayout->addWidget(area1);  // 功能区1添加到主菜单布局

    // ====================== 功能区2：统计 ======================
    QWidget *area2 = new QWidget(m_topMenu);
    area2->setFixedHeight(100);
    area2->setStyleSheet("background-color: #FFFFFF; border: 1px solid #CCCCCC; border-radius: 4px;");
    QVBoxLayout *layout2 = new QVBoxLayout(area2);
    layout2->setContentsMargins(5, 5, 5, 5);
    layout2->setSpacing(5);

    QHBoxLayout *btnLayout2 = new QHBoxLayout();
    btnLayout2->setContentsMargins(0, 0, 0, 0);
    btnLayout2->setSpacing(10);

    m_pointsHistoryBtn = new QPushButton("积分历史", area2);
    m_pointsHistoryBtn->setFixedSize(60, 60);
    m_consumptionDetailBtn = new QPushButton("消费明细", area2);
    m_consumptionDetailBtn->setFixedSize(60, 60);
    m_incomeExpenseBtn = new QPushButton("收支明细", area2);
    m_incomeExpenseBtn->setFixedSize(60, 60);

    btnLayout2->addWidget(m_pointsHistoryBtn);
    btnLayout2->addWidget(m_consumptionDetailBtn);
    btnLayout2->addWidget(m_incomeExpenseBtn);

    QLabel *label2 = new QLabel("统计", area2);
    label2->setFixedHeight(20);
    label2->setAlignment(Qt::AlignCenter);
    label2->setStyleSheet("background-color: #FFA500; color: #333333; font-weight: bold; border-radius: 2px;");

    layout2->addLayout(btnLayout2);
    layout2->addWidget(label2);
    mainMenuLayout->addWidget(area2);

    // ====================== 功能区3：充值 ======================
    QWidget *area3 = new QWidget(m_topMenu);
    area3->setFixedHeight(100);
    area3->setStyleSheet("background-color: #FFFFFF; border: 1px solid #CCCCCC; border-radius: 4px;");
    QVBoxLayout *layout3 = new QVBoxLayout(area3);
    layout3->setContentsMargins(5, 5, 5, 5);
    layout3->setSpacing(5);

    QHBoxLayout *btnLayout3 = new QHBoxLayout();
    btnLayout3->setContentsMargins(0, 0, 0, 0);
    btnLayout3->setSpacing(10);

    m_memberRechargeBtn = new QPushButton("会员充值", area3);
    m_memberRechargeBtn->setFixedSize(60, 60);
    m_rechargeStatsBtn = new QPushButton("充值统计", area3);
    m_rechargeStatsBtn->setFixedSize(60, 60);

    btnLayout3->addWidget(m_memberRechargeBtn);
    btnLayout3->addWidget(m_rechargeStatsBtn);

    QLabel *label3 = new QLabel("充值", area3);
    label3->setFixedHeight(20);
    label3->setAlignment(Qt::AlignCenter);
    label3->setStyleSheet("background-color: #90EE90; color: #333333; font-weight: bold; border-radius: 2px;");

    layout3->addLayout(btnLayout3);
    layout3->addWidget(label3);
    mainMenuLayout->addWidget(area3);

    // ====================== 功能区4：配置 ======================
    QWidget *area4 = new QWidget(m_topMenu);
    area4->setFixedHeight(100);
    area4->setStyleSheet("background-color: #FFFFFF; border: 1px solid #CCCCCC; border-radius: 4px;");
    QVBoxLayout *layout4 = new QVBoxLayout(area4);
    layout4->setContentsMargins(5, 5, 5, 5);
    layout4->setSpacing(5);

    QHBoxLayout *btnLayout4 = new QHBoxLayout();
    btnLayout4->setContentsMargins(0, 0, 0, 0);
    btnLayout4->setSpacing(10);

    m_memberConfigBtn = new QPushButton("会员配置", area4);
    m_memberConfigBtn->setFixedSize(60, 60);
    m_productPointsBtn = new QPushButton("积分设置", area4);
    m_productPointsBtn->setFixedSize(60, 60);
    m_discountConfigBtn = new QPushButton("折扣设置", area4);
    m_discountConfigBtn->setFixedSize(60, 60);

    btnLayout4->addWidget(m_memberConfigBtn);
    btnLayout4->addWidget(m_productPointsBtn);
    btnLayout4->addWidget(m_discountConfigBtn);

    QLabel *label4 = new QLabel("配置", area4);
    label4->setFixedHeight(20);
    label4->setAlignment(Qt::AlignCenter);
    label4->setStyleSheet("background-color: #87CEFA; color: #333333; font-weight: bold; border-radius: 2px;");

    layout4->addLayout(btnLayout4);
    layout4->addWidget(label4);
    mainMenuLayout->addWidget(area4);

    // ====================== 功能区5：其他 ======================
    QWidget *area5 = new QWidget(m_topMenu);
    area5->setFixedHeight(100);
    area5->setStyleSheet("background-color: #FFFFFF; border: 1px solid #CCCCCC; border-radius: 4px;");
    QVBoxLayout *layout5 = new QVBoxLayout(area5);
    layout5->setContentsMargins(5, 5, 5, 5);
    layout5->setSpacing(5);

    QHBoxLayout *btnLayout5 = new QHBoxLayout();
    btnLayout5->setContentsMargins(0, 0, 0, 0);
    btnLayout5->setSpacing(10);

    m_birthdayReminderBtn = new QPushButton("生日提醒", area5);
    m_birthdayReminderBtn->setFixedSize(60, 60);

    btnLayout5->addWidget(m_birthdayReminderBtn);

    QLabel *label5 = new QLabel("其他", area5);
    label5->setFixedHeight(20);
    label5->setAlignment(Qt::AlignCenter);
    label5->setStyleSheet("background-color: #DDA0DD; color: #333333; font-weight: bold; border-radius: 2px;");

    layout5->addLayout(btnLayout5);
    layout5->addWidget(label5);
    mainMenuLayout->addWidget(area5);
    mainMenuLayout->addStretch();

    // 3. 设置所有按钮的统一样式
    QString buttonStyle =
        "QPushButton {"
        "   background-color: #FFFFFF;"
        "   color: #333333;"
        "   border: 1px solid #CCCCCC;"
        "   border-radius: 3px;"
        "   font-size: 12px;"
        "   font-family: Microsoft YaHei;"
        "}"
        "QPushButton:hover {"  // 鼠标悬浮样式
        "   background-color: #F5F5F5;"
        "   border-color: #BBBBBB;"
        "}"
        "QPushButton:pressed {"  // 按钮按下样式
        "   background-color: #E8E8E8;"
        "   border-color: #AAAAAA;"
        "}";

    // 批量给所有按钮设置样式
    QList<QPushButton*> buttons = m_topMenu->findChildren<QPushButton*>();
    for (QPushButton *btn : qAsConst(buttons)) {
        btn->setStyleSheet(buttonStyle);
    }

    // 4. 连接按钮点击信号到堆叠窗口页面切换（索引与页面顺序对应）
    connect(m_memberInfoBtn, &QPushButton::clicked, this, [this]() {
        if (m_stackedWidget) m_stackedWidget->setCurrentIndex(0);
    });
    connect(m_addMemberBtn, &QPushButton::clicked, this, [this]() {
        if (m_stackedWidget) m_stackedWidget->setCurrentIndex(1);
    });
    connect(m_pointsHistoryBtn, &QPushButton::clicked, this, [this]() {
        if (m_stackedWidget) m_stackedWidget->setCurrentIndex(2);
    });
    connect(m_consumptionDetailBtn, &QPushButton::clicked, this, [this]() {
        if (m_stackedWidget) m_stackedWidget->setCurrentIndex(3);
    });
    connect(m_incomeExpenseBtn, &QPushButton::clicked, this, [this]() {
        if (m_stackedWidget) m_stackedWidget->setCurrentIndex(4);
    });
    connect(m_memberRechargeBtn, &QPushButton::clicked, this, [this]() {
        if (m_stackedWidget) m_stackedWidget->setCurrentIndex(5);
    });
    connect(m_rechargeStatsBtn, &QPushButton::clicked, this, [this]() {
        if (m_stackedWidget) m_stackedWidget->setCurrentIndex(6);
    });
    connect(m_memberConfigBtn, &QPushButton::clicked, this, [this]() {
        if (m_stackedWidget) m_stackedWidget->setCurrentIndex(7);
    });
    connect(m_productPointsBtn, &QPushButton::clicked, this, [this]() {
        if (m_stackedWidget) m_stackedWidget->setCurrentIndex(8);
    });
    connect(m_discountConfigBtn, &QPushButton::clicked, this, [this]() {
        if (m_stackedWidget) m_stackedWidget->setCurrentIndex(9);
    });
    connect(m_birthdayReminderBtn, &QPushButton::clicked, this, [this]() {
        if (m_stackedWidget) m_stackedWidget->setCurrentIndex(10);
    });
}
